<?php
	class WorkflowModel extends CI_Model {
		// 表名
		static $workflowTable = "school_workflow";
		static $workflowApplicationTable = 'school_workflow_application';

	    public function __construct() {
	        parent::__construct();
	    }

	    // 获取工作流流程信息
	    public function get_workflow_info($workflowId) {
	    	$where = "`id`='{$workflowId}'";
	    	$workflowInfo = $this->db->where($where)->get(self::$workflowTable)->row_array();
	    	return $workflowInfo;
	    }

	    // 获取工作流流程列表
	    public function get_workflow_list($params = ""){
	    	$where = "1 AND 1";
	    	// 分页
	    	if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
	    	// 学校id
	    	if(isset($params['schoolId'])) $where .= " AND `school_id`='{$params['schoolId']}'";
	    	if(isset($params['campusId'])) $where .= " AND `campus_id`='{$params['campusId']}'";
	    	// 删除状态
			if(isset($params['isDelete'])) $where .= " AND `is_delete`='{$params['isDelete']}'";
			// 过滤状态
			if(isset($params['state']) && $params['state'] === 'normal') $where .= " AND `workflow_form` <> '' AND `workflow_process` <> ''";

	    	$this->db->where($where);
	    	// 是否只获取条数
	    	if(isset($params['getRowNumber'])) {
	    		$result = $this->db->count_all_results(self::$workflowTable);
	    	} else {
	    		$result = $this->db->get(self::$workflowTable)->result_array();	
	    	}
	    	return $result;
		}

	    // 获取工作流申请详情
	    public function get_workflow_application_info($applicationId) {
	    	$where = "`id`='{$applicationId}'";
	    	$applicationInfo = $this->db->where($where)->get(self::$workflowApplicationTable)->row_array();
	    	return $applicationInfo;
	    }
		
		// 获取工作流申请列表
		public function get_workflow_application_list($params = ""){
	    	// 分页
			if(isset($params['pageStart']) && isset($params['pageSize'])) $this->db->limit($params['pageSize'], $params['pageStart']);
			// 默认按发起时间倒序
			if(isset($params['orderBy'])) {
				$this->db->order_by($params['orderBy']['order'], $params['orderBy']['sort']);
			} else {
				$this->db->order_by('createtime', 'DESC');
			}
	    	// 学校id
			if(isset($params['schoolId'])) $this->db->where('school_id', $params['schoolId']);
			// 校区id
			if(isset($params['campusId'])) $this->db->where('campus_id', $params['campusId']);
	    	// 删除状态
			if(isset($params['isDelete'])) $this->db->where('is_delete', $params['isDelete']);
			// 申请人用户id
			if(isset($params['userId'])) $this->db->where('user_id', $params['userId']);
			// 处理人用户id
			if(isset($params['handleUserId'])) $this->db->where('handle_user_id', $params['handleUserId']);
			// 处理历史用户id
			if(isset($params['handleHistoryUserId'])) $this->db->like('handle_history', 'user_id":"' . $params['handleHistoryUserId'] . '"');
			// 工作流id
			if(isset($params['workflow_id'])) $this->db->where('workflow_id', $params['workflow_id']);
			// 状态
			if(isset($params['state'])) $this->db->where('state', $params['state']);
			// 发起时间
			if(isset($params['createtime'])) $this->db->like('createtime', $params['createtime']);
	    	// 是否只获取条数
	    	if(isset($params['getRowNumber'])) {
	    		$result = $this->db->count_all_results(self::$workflowApplicationTable);
	    	} else {
	    		$result = $this->db->get(self::$workflowApplicationTable)->result_array();	
	    	}
	    	return $result;
		}
	}